package data_structure;

public class leetcode_0203 {
    public ListNode removeElements(ListNode head, int val) {
        ListNode p=head;
        while(p!=null&&p.val==val) p=p.next;
        if(p==null) {
            return null;
        }
        head=p;
        ListNode q=head;
        p=p.next;
        while(p!=null){
            if(p.val==val){
                q.next=p.next;
                p=p.next;
            }
            else{
                p=p.next;
                q=q.next;
            }
        }
        return head;
    }
}
class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }